package gui;


import javax.swing.*;

import code.KeyStoreManager;
import code.Settings;

import java.awt.*;
import java.io.*;
import java.awt.image.*;

/**
 * Background image demo
 */

public class PersonalViewCertificate extends JPanel {

	Color c= new Color(219, 191, 144);

	Image bgImage=null;
	
	JLabel jLabel1 = new JLabel("Full Name :");
	JLabel jLabel2 = new JLabel("Organizational Unit :");
	JLabel jLabel3 = new JLabel("Organization :");
	JLabel jLabel4 = new JLabel("City :");
	JLabel jLabel5 = new JLabel("State or Provience :");
	JLabel jLabel6 = new JLabel("Country :");
	JLabel jLabel7 = new JLabel("Alias :");
	JLabel jLabel8 = new JLabel("Key Size :");
	JLabel jLabel9 = new JLabel("Not Before (Date) :");
	JLabel jLabel10 = new JLabel("Not After (Date) :");
	JLabel jLabel11 = new JLabel("Back");

	JTextField jTextField1 = new JTextField();
	JTextField jTextField2 = new JTextField();
	JTextField jTextField3 = new JTextField();
	JTextField jTextField4 = new JTextField();
	JTextField jTextField5 = new JTextField();
	JTextField jTextField6 = new JTextField();
	JTextField jTextField7 = new JTextField();
	JTextField jTextField8 = new JTextField();
	JTextField jTextField9 = new JTextField();
	JTextField jTextField10 = new JTextField();

	JTextField field = new JTextField(20);
	String message="";
	ATOCOmain atocoframe;
	private KeyStoreManager ksm;
	String prevclassvalue;
	JPanel prevclass;
	
	public PersonalViewCertificate(ATOCOmain atocoframe, String certAlias, JPanel prevclass, String prevclassvalue) {
		super(null);
		// Every Keystore operation must use this one.
		ksm = new KeyStoreManager(
				(String)Settings.global.get("personalKeyStoreFile"), 
				(String)Settings.global.get("personalKeyStorePassword"));
		if (ksm.getKeyStore() == null) {
        	System.out.println("Keystore file not found. Creating one.");
        	ksm.createKeyStore(
        			(String)Settings.global.get("personalKeyStoreFile"), 
    				(String)Settings.global.get("personalKeyStorePassword"));
        }
		
		this.atocoframe=atocoframe;
		this.prevclass=prevclass;
		this.prevclassvalue=prevclassvalue;
		
		getFileImage("images/en/parchment.jpg");
		jLabel1.setBounds(new Rectangle(90, 105, 107, 16));
		jLabel2.setBounds(new Rectangle(90, 135, 119, 16));
		jLabel3.setBounds(new Rectangle(90, 165, 107, 16));
		jLabel4.setBounds(new Rectangle(90, 195, 107, 16));
		jLabel5.setBounds(new Rectangle(90, 225, 113, 16));
		jLabel6.setBounds(new Rectangle(90, 255, 107, 16));
		jLabel7.setBounds(new Rectangle(90, 285, 107, 16));
		jLabel8.setBounds(new Rectangle(90, 315, 107, 16));
		jLabel9.setBounds(new Rectangle(90, 345, 109, 16));
		jLabel10.setBounds(new Rectangle(90, 375, 107, 16));
		jLabel11.setBounds(new Rectangle(90, 405, 173, 37));
		jLabel11.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
		jLabel11.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				setprevclass();
			}
		});

		jTextField1.setBounds(new Rectangle(210, 105, 115, 20));
		jTextField2.setBounds(new Rectangle(210, 135, 115, 20));
		jTextField3.setBounds(new Rectangle(210, 165, 115, 20));
		jTextField4.setBounds(new Rectangle(210, 195, 115, 20));
		jTextField5.setBounds(new Rectangle(210, 225, 115, 20));
		jTextField6.setBounds(new Rectangle(210, 255, 115, 20));
		jTextField7.setBounds(new Rectangle(210, 285, 115, 20));
		jTextField8.setBounds(new Rectangle(210, 315, 115, 20));
		jTextField9.setBounds(new Rectangle(210, 345, 115, 20));
		jTextField10.setBounds(new Rectangle(210, 375, 115, 20));

		jTextField1.setOpaque(false);
		jTextField2.setOpaque(false);
		jTextField3.setOpaque(false);
		jTextField4.setOpaque(false);
		jTextField5.setOpaque(false);
		jTextField6.setOpaque(false);
		jTextField7.setOpaque(false);
		jTextField8.setOpaque(false);
		jTextField9.setOpaque(false);
		jTextField10.setOpaque(false);

		getCertFields(certAlias);

		jTextField1.setEditable(false);
		jTextField2.setEditable(false);
		jTextField3.setEditable(false);
		jTextField4.setEditable(false);
		jTextField5.setEditable(false);
		jTextField6.setEditable(false);
		jTextField7.setEditable(false);
		jTextField8.setEditable(false);
		jTextField9.setEditable(false);
		jTextField10.setEditable(false);

		jTextField1.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
		jTextField2.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
		jTextField3.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
		jTextField4.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
		jTextField5.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
		jTextField6.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
		jTextField7.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
		jTextField8.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
		jTextField9.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));
		jTextField10.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, c));

		add(jLabel1);
		add(jLabel2);
		add(jLabel3);
		add(jLabel4);
		add(jLabel5);
		add(jLabel6);
		add(jLabel7);
		add(jLabel8);
		add(jLabel9);
		add(jLabel10);
		add(jLabel11);
		add(jTextField1);
		add(jTextField2);
		add(jTextField3);
		add(jTextField4);
		add(jTextField5);
		add(jTextField6);
		add(jTextField7);
		add(jTextField8);
		add(jTextField9);
		add(jTextField10);
		
	}
	public void paintComponent(Graphics g) {
		if (bgImage!=null) {
			g.drawImage(bgImage,0,0,this);
		} else {
			g.drawString(message,40,40);
		}
	}
	private void getFileImage(String filePath) {
		try {
			FileInputStream in = new FileInputStream(filePath);
			byte [] b=new byte[in.available()];
			in.read(b);
			in.close();
			bgImage=Toolkit.getDefaultToolkit().createImage(b);
			MediaTracker mt=new MediaTracker(this);
			mt.addImage(bgImage,0);
			mt.waitForAll();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void getCertFields(String certAlias) {
		String cert = ksm.getCertificate(certAlias).toString();
		//[ [   Version: V1   Subject: C=dgdrg, ST=drgdrg, L=drgdg, O=drgdrg, OU=drgdrg, CN=sfsf dgdgr   Signature Algorithm: MD5withRSA, OID = 1.2.840.113549.1.1.4    Key:  Sun RSA public key, 1024 bits   modulus: 129565519865620128216213992343497354762030437801733956019435881723201120751904800222896079252672981018023332865792033808522930961124512930705527186800331181334639900704300471473944022814748488116845850490093906847832128279107073452255184394877636959156955031678896609684953563971364684578572177330447421631007   public exponent: 65537   Validity: [From: Fri Jan 01 00:00:00 EET 2010,                To: Sat Jan 01 00:00:00 EET 2011]   Issuer: C=dgdrg, ST=drgdrg, L=drgdg, O=drgdrg, OU=drgdrg, CN=sfsf dgdgr   SerialNumber: [    01]  ]   Algorithm: [MD5withRSA]   Signature: 0000: B1 B8 72 5A 30 37 7A 67   BB B2 C5 DB A1 67 39 72  ..rZ07zg.....g9r 0010: B3 72 42 3C EB 5A A9 1C   46 66 56 0F 3E 4F 73 47  .rB<.Z..FfV.>OsG 0020: E0 F5 E8 80 8F A0 D1 CF   29 73 2F 15 75 49 9E 39  ........)s/.uI.9 0030: 59 2F C7 B1 98 2F 8D A5   D9 4D 18 46 0E 1B 83 99  Y/.../...M.F.... 0040: AD 6A 86 01 CF 7B 6B 52   64 55 09 F4 67 C3 DB 49  .j....kRdU..g..I 0050: 48 55 E6 87 DE B8 B4 4C   5D 2E 1F DF C1 24 DC 81  HU.....L]....$.. 0060: 3D 09 74 97 CF FF 27 DA   57 3C B7 05 FB F9 40 7D  =.t...'.W<....@. 0070: 6A 22 B3 FC E1 E7 C7 97   86 3B E9 6A 19 A1 9D 68  j".......;.j...h  ]
		String flName = cert.substring(cert.indexOf("CN=")+3, cert.indexOf("Signature Algorithm:"));
		String ou = cert.substring(cert.indexOf("OU=")+3, cert.indexOf(", CN="));
		String o = cert.substring(cert.indexOf("O=")+2, cert.indexOf(", OU="));
		String l = cert.substring(cert.indexOf("L=")+2, cert.indexOf(", O="));
		String st = cert.substring(cert.indexOf("ST=")+3, cert.indexOf(", L="));
		String c = cert.substring(cert.indexOf("C=")+2, cert.indexOf(", ST="));
		String alias = certAlias;
		String size = cert.substring(cert.indexOf("RSA public key,")+15, cert.indexOf("bits", cert.indexOf("Sun RSA public key")));
		String from = cert.substring(cert.indexOf("From:")+5, cert.indexOf(",",cert.indexOf("From:")));
		String to = cert.substring(cert.indexOf("To:")+3, cert.indexOf("]",cert.indexOf("From:")));
		
//		jTextField.setText(flName.trim());
		jTextField1.setText(flName.trim());
		jTextField2.setText(ou.trim());
		jTextField3.setText(o.trim());
		jTextField4.setText(l.trim());
		jTextField5.setText(st.trim());
		jTextField6.setText(c.trim());
		jTextField7.setText(alias.trim());
		jTextField8.setText(size.trim());
		jTextField9.setText(from.trim());
		jTextField10.setText(to.trim());
	}
	
	private void setprevclass() {
		if(prevclassvalue.equalsIgnoreCase("PersonalListCert")){
			PersonalListCertificate list=(PersonalListCertificate)prevclass;
			atocoframe.contentPane(list);
		}
	}
	
//	public static void main(String[] args) {
//		PersonalViewCertificate bgd=new PersonalViewCertificate();
//		JFrame f=new JFrame("Background image");
//		f.setBounds(10,10,415,545);
//		f.getContentPane().setLayout(new GridLayout());
//		f.getContentPane().add(bgd);
//		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		f.setVisible(true);
//	}
}
